package com.pay.ioopos.fragment;

import static com.pay.ioopos.App.DEV_IS_306_308;
import static com.pay.ioopos.App.DEV_IS_FACE;
import static com.pay.ioopos.Constants.INTENT_PARAM_CASE;
import static com.pay.ioopos.Constants.INTENT_PARAM_CODE;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.zxing.BarcodeFormat;
import com.pay.ioopos.R;
import com.pay.ioopos.fragment.support.Scheduled;
import com.pay.ioopos.support.card.CardFactory;
import com.pay.ioopos.support.scan.ScanCase;
import com.pay.ioopos.support.scan.ScanLife;
import com.pay.ioopos.support.scan.ScanListener;
import com.pay.ioopos.support.scan.s1601.ScanS1601Qrcode;
import com.pay.ioopos.support.scan.sp308.ScanCamera;
import com.pay.ioopos.support.scan.weixin.MyWxPayFace;
import com.pay.ioopos.support.scan.weixin.ScanWxOfflineQrcode;
import com.pay.ioopos.support.scan.weixin.ScanWxOnlineQrcode;
import com.pay.ioopos.support.serialport.Cmd;
import com.pay.ioopos.support.serialport.CmdCase;
import com.pay.ioopos.support.serialport.OnMutipleListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


public class AbstractScanFragment extends AbstractNetworkFragment implements Scheduled {
    private ScanListener listener;
    private final BarcodeFormat format;
    private Future<?> future;
    public AbstractScanFragment() {
        this(null);
    }
    public AbstractScanFragment(BarcodeFormat format) {
        this.format = format;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_scan, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (null != future) {
            future.cancel(true);
        }
        future = ownSubmit(() -> {
            try {
                bindToLifecycle();
            } catch (Throwable e) {
                if (null != listener) {
                    listener.onError(e.getMessage());
                }
            }
        });
    }

    public void setListener(ScanListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean useNetwork() {
        return false;
    }

    private void bindToLifecycle() {
        List<ScanCase> cases = new ArrayList<>();

        // 外接扫码器s1601
        cases.add(new ScanS1601Qrcode(getContext()));

        // 串口扫码
        CmdCase cmdCase = CardFactory.getCmdCase();
        if (null != cmdCase) {
            Cmd cmd = CardFactory.uidOrQrcodeRead(2, new OnMutipleListener() {
                @Override
                public Cmd onSuccess(int flag, String value) {
                    Intent intent = new Intent();
                    intent.putExtra(INTENT_PARAM_CASE, ScanCase.QRCODE);
                    intent.putExtra(INTENT_PARAM_CODE, value);
                    listener.onScan(intent);
                    return null;
                }

                @Override
                public Cmd onFail(int flag, String value) {
                    listener.onError(value);
                    return null;
                }
            }, cmdCase);
            if (null != cmd) {
                cmdCase.setRootCmd(cmd);
                cases.add(cmdCase);
            }
        }

        if (DEV_IS_306_308) {
            cases.add(new ScanCamera(format));
        }

        if (DEV_IS_FACE) {
            if (MyWxPayFace.IS_OFFLINE) {
                cases.add(new ScanWxOfflineQrcode());
            } else {
                cases.add(new ScanWxOnlineQrcode());
            }
        }

        ScanCase scanCase = new ScanLife(cases);
        scanCase.setScanListener(listener);
        scanCase.bindToLifecycle(this);
    }

}
